package com.zxl.admin.controller.role;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.zxl.admin.service.RoleService;
import com.zxl.common.Page;
import com.zxl.common.Result;
import com.zxl.common.model.bo.role.RoleAdd;
import com.zxl.common.model.pojo.SysRole;
import com.zxl.common.model.pojo.SysRoleApi;
import com.zxl.common.model.query.role.RoleQuery;
import com.zxl.common.model.vo.order.OrderPageVo;
import com.zxl.common.model.vo.role.RoleApiGroupBean;
import com.zxl.common.service.SysRoleApiService;
import com.zxl.common.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

/**
 * @author zxw
 * @date 2023/4/6
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysRoleApiService sysRoleApiService;



    @PostMapping("/add")
    @ApiOperation("角色添加")
    public Result add(@RequestBody @Valid RoleAdd roleAdd){
        String name = roleAdd.getName();
        String apiStr = roleAdd.getApiStr();
        roleService.addRole(name, apiStr);
        return Result.success();

    }



    @PostMapping("/mod")
    @ApiOperation("角色修改")
    public Result mod(@RequestBody @Valid RoleAdd roleAdd) {
        roleService.modRole(roleAdd);
        return Result.success();

    }


    @PostMapping("/del")
    @ApiOperation("角色删除")
    @Transactional(rollbackFor = Exception.class)
    public Result del(@RequestParam @ApiParam(value = "角色id") String id){
        sysRoleService.removeById(id);
        sysRoleApiService.remove(new QueryWrapper<SysRoleApi>()
                .eq("role_id", id));
        return Result.success();
    }



    @GetMapping("/list")
    @ApiOperation("角色列表")
    public Result<Page<SysRole>> list(RoleQuery roleQuery){
        List<SysRole> list = roleService.list(roleQuery);
        Page<SysRole> page = Page.pageInfoConvert(new PageInfo(list));
        return new Result<>(page);
    }



    @GetMapping("/getRoleApi")
    @ApiOperation("角色权限列表")
    public Result<List<RoleApiGroupBean>> getRoleApi(@RequestParam @ApiParam(value = "角色id", required = true) String id){
        List<RoleApiGroupBean> roleApiList = roleService.getRoleApi(id);

        return new Result<>(roleApiList);
    }

}
